<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<style type="text/css">
#editForm
</style>
<!--添加或修改体检卡种类弹窗-->
<div class="easyui-dialog" id="editCoupon_dialog" title="添加优惠券" data-options="closed:true,modal:true,width:450,height:450">
    <form id="editForm" method="post">
        <input id="couponId" name="id" hidden>
        <table align="center" style="margin-top: 20px" >
            <tr style="border: 10px">
                <td><lable for="name">优惠券名称:</lable></td>
                <td><input class="easyui-validatebox" name="name" data-options="required:true" /></td>
            </tr>
            <tr >
                <td><lable for="intro">描述:</lable></td>
                <td><input class="easyui-validatebox" name="intro" data-options="required:true"/></td>
            </tr>
            <tr >
                <td><lable for="providerType">提供者:</lable></td>
                <td>
                    <span>商家:</span><input  id="provider0" name="providerType" type="radio" value="0" />
                    <span>平台:</span><input  id="provider1" name="providerType" type="radio" value="1" />
                </td>
            </tr>
            <tr >
                <td><lable for="suitType">适用分类:</lable></td>
                <td><!--0全平台、1平台分类、2平台商家、3平台内产品、4商家全场、5商家内分-->
                    <select id="cardclass_id" class="easyui-combobox" data-options="panelHeight:'auto',required:true" name="suitType"  style="width: 150px"/>
                    <option value="0">全平台</option>
                    <option value="1">平台分类</option>
                    <option value="2">平台商家</option>
                    <option value="3">平台内产品</option>
                    <option value="4">商家全场</option>
                    <option value="5">商家内分类</option>
                    </select>
                </td>
            </tr>
            <tr >
                <td><lable for="platform">平台:</lable></td>
                <td><!--1全平台，2APP，3手机wap，4pc,5小程序-->
                    <select id="platform_id" class="easyui-combobox" data-options="panelHeight:'auto',required:true" name="platform"  style="width: 150px"/>
                    <option value="1">全平台</option>
                    <option value="2">APP</option>
                    <option value="3">手机wap</option>
                    <option value="4">PC</option>
                    <option value="5">小程序</option>
                    </select>
                </td>
            </tr>
            <tr >
                <td><lable for="system">系统:</lable></td>
                <td><!--1祺云，2农信-->
                    <select id="system_id" class="easyui-combobox" data-options="panelHeight:'auto',required:true" name="system"  style="width: 150px"/>
                    <option value="1">祺云</option>
                    <option value="2">农信</option>
                    </select>
                </td>
            </tr>
            <tr >
                <td><lable for="type">优惠券类型:</lable></td>
                <td>
                    <span>满减:</span><input  id="type0" name="type" type="radio" value="0" />
                    <span>折扣:</span><input  id="type1" name="type" type="radio" value="1" />
                    <span>现金:</span><input  id="type3" name="type" type="radio" value="2" />
                </td>
            </tr>
            <tr >
                <td><lable for="faceVal">满减额度:</lable></td>
                <td><input class="easyui-validatebox" name="subtractionMoney" data-options="required:true"/></td>
            </tr>
            <tr >
                <td><lable for="money">满减/现金金额:</lable></td>
                <td><input class="easyui-validatebox" name="money" data-options="required:true"/></td>
            </tr>
            <tr >
                <td><lable for="discount">折扣:</lable></td>
                <td><input class="easyui-validatebox" name="discount" data-options="required:true" /></td>
            </tr>
            <tr >
                <td><lable for="maxDiscountMoney">最大折扣金额:</lable></td>
                <td><input class="easyui-validatebox" name="maxDiscountMoney" data-options="required:true"/></td>
            </tr>
            <tr >
                <td><lable for="repertory">发放数量:</lable><input  name="remainRepertory" hidden/></td>
                <td><input class="easyui-validatebox" name="repertory" data-options="required:true"/></td>
            </tr>
            <tr >
                <td><lable for="startTime">有效开始时间:</lable></td>
                <td><input id="begintime" class="easyui-validatebox"  />
                    <input  name="startTime" hidden/>
                </td>
            </tr>
            <tr >
                <td><lable for="endTime">有效结束时间:</lable></td>
                <td><input id="endtime" class="easyui-validatebox" />
                    <input  name="endTime" hidden/>
                </td>
            </tr>
        </table>
        <div style="margin-top: 20px;text-align: center">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="saveCoupon()">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="$('#editCoupon_dialog').dialog('close')">取消</a>
        </div>
    </form>
</div>

<script type="text/javascript">
    var rowid;
    function editAction(value,row){
        var str = '';
        if(value=="add"){
            str += $.formatString('<a href="javascript:void(0)" class="category-easyui-linkbutton-add" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editCategory(\'{0}\',\'add\',this);" >添加</a>', row.id);
        }else{
            str += $.formatString('<a href="javascript:void(0)" class="category-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="editCategory(\'{0}\',\'del\',this);" >删除</a>', row.id);
        }
        return str;
    }
    //联动事件
    function rowchange(id){
        rowid = id;
        $("#category_datagrid").datagrid('clearSelections');
        var queryParams = {};
        queryParams.id = id;
        queryParams.type = 1;
        //重新加载分类列表
        var options = $("#category_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#category_datagrid").datagrid('options').queryParams = queryParams;
        $("#category_datagrid").datagrid('reload');
        //重新加载排除分类列表
        var options2 = $("#discategory_datagrid").datagrid('getPager').data("pagination").options;
        options2.pageNumber = 1;
        $("#discategory_datagrid").datagrid('options').queryParams = queryParams;
        $("#discategory_datagrid").datagrid('reload');
        //重新加载适用商家列表
        var options3 = $("#Seller_datagrid").datagrid('getPager').data("pagination").options;
        options3.pageNumber = 1;
        $("#Seller_datagrid").datagrid('options').queryParams = queryParams;
        $("#Seller_datagrid").datagrid('reload');
        //重新加载排除商家列表
        var options4 = $("#disSeller_datagrid").datagrid('getPager').data("pagination").options;
        options4.pageNumber = 1;
        $("#disSeller_datagrid").datagrid('options').queryParams = queryParams;
        $("#disSeller_datagrid").datagrid('reload');
        //重新加载适用产品列表
        var options5 = $("#Product_datagrid").datagrid('getPager').data("pagination").options;
        options5.pageNumber = 1;
        $("#Product_datagrid").datagrid('options').queryParams = queryParams;
        $("#Product_datagrid").datagrid('reload');
        //重新加载排除产品列表
        var options6 = $("#disProduct_datagrid").datagrid('getPager').data("pagination").options;
        options6.pageNumber = 1;
        $("#disProduct_datagrid").datagrid('options').queryParams = queryParams;
        $("#disProduct_datagrid").datagrid('reload');
        //重置所有下拉框
        $("#filsPanel").find("select").val(1);
    }
    //时间表单
    $('#begintime').datetimebox({
        value: '3/4/2010 2:3',
        required: true,
        showSeconds: false
    });

    $('#endtime').datetimebox({
        value: '3/4/2010 2:3',
        required: true,
        showSeconds: false
    });

    var flag = "add";
    //打开添加修改弹层
    function openEditBox(str){
        flag = str;
        $("#begintime").next().find(".textbox-text").val("");
        $("#endtime").next().find(".textbox-text").val("");
        if(str=="update"){
            var selectrow = $("#coupon_datagrid").datagrid("getSelected");
            if(selectrow==null){
                $.messager.alert("提示","请选择要修改的优惠券","info");
                return;
            }
            $("#editForm").form("load",selectrow);
            $("#provider"+selectrow.providerType).attr("checked","checked");
            $("#type"+selectrow.type).attr("checked","checked");
            $("#begintime").next().find(".textbox-text").val(formatDateBoxFull(selectrow.startTime));
            $("#endtime").next().find(".textbox-text").val(formatDateBoxFull(selectrow.endTime));
        }else{
            <!--默认平台提供，满减类型-->
            $("#editForm").form("clear");
            $("#provider1").attr("checked","checked");
            $("#type0").attr("checked","checked");
        }
        $("#editCoupon_dialog").dialog("open");
    }
    //删除
    function deleteCoupon() {
        var select = $("#coupon_datagrid").datagrid("getSelected");
        if(select == null){
            $.messager.alert("提示","请选择优惠券","info");
            return;
        }
        $.messager.confirm("确认","确定要删除吗?\n确定删除将连优惠券信息一同删除",function(yes){
            if(yes){
                commonDelete("${path}/coupon/delete.do",select.id,"coupon_datagrid");
            }
        });
    }
    //重置刷新
    function refconsumeDg(){
        $("#settleStatus").val("");
        $("#begin").val("");
        $("#end").val("");
        var queryParams = {};
        var options = $("#coupon_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#coupon_datagrid").datagrid('options').queryParams = queryParams;
        $("#coupon_datagrid").datagrid('reload');
    }

    //下拉筛选提供者状态
    function comboxchange(){
        var type = $("#settleStatus").val();
        var queryParams = {};
        queryParams.type=type;
        var options = $("#coupon_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#coupon_datagrid").datagrid('options').queryParams = queryParams;
        $("#coupon_datagrid").datagrid('reload');
    }
    //搜索
    function selectCoupon(){
        var begin = $("#begin").val();
        var end = $("#end").val();
        var type = $("#settleStatus").val();
        var queryParams = {};
        queryParams.type=type;
        queryParams.begin=begin;
        queryParams.end=end;
        var options = $("#coupon_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#coupon_datagrid").datagrid('options').queryParams = queryParams;
        $("#coupon_datagrid").datagrid('reload');
    }
    //保存优惠券信息
    function saveCoupon(){
        var url = "";
        var str = "";
        if(flag == 'add'){
            url = "${path}/coupon/add.do";
            str = "添加";
        }else if(flag == 'update'){
            url = "${path}/coupon/edit.do";
            str = "修改";
        }
        var star = $("#begintime").next().find(".textbox-text").val().replace(/-/g,"/");
        var end = $("#endtime").next().find(".textbox-text").val().replace(/-/g,"/");
        $("input[name=startTime]").val(star);
        $("input[name=endTime]").val(end);
        $("#editForm").form("submit",{
            url:url,
            onSubmit:function(){
                var valid = $(this).form("validate");
                if (!valid) {
                    $.messager.progress("close");
                }
                return valid;
            },
            success:function(data){
                $.messager.progress("close");
                data = JSON.parse(data);
                if(data.success){
                    $("#editCoupon_dialog").dialog("close");
                    $.messager.show({
                        title:str +"成功",msg:str +"成功!",
                        timeout:600,style:"left:30%;top:10%",
                        showType:"fade"
                    });
                    $("#coupon_datagrid").datagrid("reload");
                }else{
                    $.messager.alert(str +"失败", data.msg,"error");
                }
            }
        });
    }
    //子表的筛选
    function comboxSelect(value,tableId){
        var table = $("#"+tableId);
        var queryParams = {};
        queryParams.id = rowid;
        queryParams.type = value;
        var options = table.datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        table.datagrid('options').queryParams = queryParams;
        table.datagrid('reload');
    }
    //编辑适用
    function editCategory(id,type,obj){
        var table = $(obj).parents(".datagrid-view").children("table");
        var tableid = table.attr("id");
        var editurl="";
        switch(tableid){
            case "category_datagrid":editurl="/coupon/editCategory.do";break;
            case "discategory_datagrid":editurl="/coupon/editdisCategory.do";break;
            case "Seller_datagrid":editurl="/coupon/editSeller.do";break;
            case "disSeller_datagrid":editurl="/coupon/editdisSeller.do";break;
            case "Product_datagrid":editurl="/coupon/editProduct.do";break;
            default:editurl="/coupon/editdisProduct.do";break;
        }
        $.ajax({
            type:"POST",dataType:"JSON",
            url:editurl,
            data:{couponId:rowid,editId:id,type:type},
            success:function(data){
                if(data.success){
                    table.datagrid("reload");
                }else{
                    $.messager.alert("操作失败", data.msg,"error");
                }
            }
        });
    }
    //查询套餐
    function Searchproduct(val) {
        console.log(this);
        var table = $(this).parents(".datagrid-wrap").children(".datagrid-view").children("table");
        val = val.trim();
        if(val.length==0){return;}
        var type = $(this).prev().val();
        var queryParams = {};
        queryParams.id = rowid;
        queryParams.keyWord = val;
        queryParams.type = type;
        var options = table.datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        table.datagrid('options').queryParams = queryParams;
        table.datagrid('reload');
    }
    //删除公共调用方法
    function commonDelete(url,id,datagrid){
        $.messager.progress();
        $.ajax({
            url:url,
            type:"post",
            data:{id : id},
            dataType:"json",
            success:function(data){
                $.messager.progress("close");
                if(data.success){
                    $.messager.show({
                        title:"删除成功",
                        msg:"删除成功!",
                        timeout:600,
                        style:"left:30%;top:10%",
                        showType:"fade"
                    });
                    $("#"+datagrid).datagrid("reload");
                    $("#"+datagrid).datagrid('clearSelections');
                }else{
                    $.messager.alert("删除失败", data.msg,"error");
                }
            },
            error:function(){
                $.messager.progress("close");
                $.messager.alert("删除失败", "服务器连接失败!","error");
            }
        });
    }
</script>

